<?php

/*
 * Контроллер основных страниц админ-панели
 */

namespace APPS\ADMIN\CONTROLLERS;

use T4\FrontController;

class Index extends Controller {

	public function getMenu() {

		$menu = array();

		$applications = array_keys(\T4\Application::getAll());
		foreach ($applications as $application) {
			$application = \T4\Application::getInstance($application);
			$appMenu = $application->getAdminPages();
			if (!empty($appMenu)) {
				$el = &$menu[];
				$el['url'] = '/admin/' . $application->name;
				$el['title'] = $application->getTitle();
				foreach ($appMenu as $key => $value) {
					$el2 = &$el['menu'][];
					$el2['url'] = $key;
					$el2['title'] = $value;
				}
			}
		}

		return $menu;
		
	}

	public function preAction() {
		parent::preAction();
		$this->view->assign('menu', $this->getMenu());
	}

	/**
	 * Главная страница админ-панели
	 */
	public function actionIndex() {
		$this->view->assign(
				array(
					'content' => 'Admin area test...'
				)
		);
	}

	public function actionApplication() {
		
		$content = '';
		$errors = array();

		try {

			$application = \T4\Application::getInstance($this->parameters->application);
			$result =
                            FrontController::call(
                                $application->name, 
                                'ADMIN\\' . ( $this->parameters->controller ?: 'Index' ), 
                                $this->parameters->action ?: 'default'
			);
                        $output = new \T4\OUTPUT\HTML(
                            $application->path . '/templates/admin/' . 
                            ( $this->parameters->controller ?: 'Index' ) . '/' . 
                            ( $this->parameters->action ?: 'Default' ) . '.html'
                        );
                        $content = $output->prepare($result);
			
		} catch (\T4\Exception $e) {
			$errors[] = $e->getMessage();
		};
		
		$this->view->assign('content', $content);
		$this->view->assign('errors',  $errors);
		
	}

}